/**
 * RequestHelper
 */

  import eventHelper from "./EventHelper";

  var tool = require("./tool");
  var consts = require("./consts");
  var uuidHelper = require("./UuidHelper");
  var RequestHelper = (function (){
      var urls = window.is.CONST.urls;
      var permission = window.is.options.permission;

      function getUrlByExtraParams(url, extraParams){
          if (!tool.isEmptyObject(extraParams)){
              for (var key in extraParams){
                  if (url.indexOf("?") == -1){
                      url += "?";
                  } else {
                      url += "&";
                  }
                  url += key + "=" + extraParams[key];
              }
          }
          return url;
      }

      function getRemovedAttrsParams(params){
          var param, output;
          if (tool.isString(params)){
              param = JSON.parse(params);
              if (tool.isArray(param)){
                  param = tool.removeExtraAttrs(param);
              // object
              } else {
                  param = tool.removeExtraAttrs([param]);
                  param = param[0];
              }
              output = JSON.stringify(param);
          } else if(tool.isArray(params)) {
              output = tool.removeExtraAttrs(params);
          // object
          } else {
              param = tool.removeExtraAttrs([params]);
              output = param[0];
          }
          return output;
      }

      function RequestHelper(type, method, params, extraParams, success, error, useParam, beforeSend){
            useParam = !!useParam;
            this.url = this.urlMapping[type];
            this.method = method || "post";
            this.params = useParam ? params : JSON.stringify(params);
            this.extraParams = extraParams || {};
            this.success = success || function (){};
            this.beforeSend = beforeSend || function (){};
            this.error = error || function (){};
            this.permission = permission;
            this.uuid = uuidHelper.generate();
        }

        RequestHelper.prototype.urlMapping = {
            "category": urls.categories,
            "importResource": urls.import_other_resource,
            "importArResource": urls.import_ar_resource,
            "page": urls.page,
            "pageJump": urls.page_jump,
            "pages": urls.pages,
            "pageTimelines": urls.page_timelines,
            "permission": urls.permission,
            "resource": urls.resources,
            "threedState": urls.get_unity3d_state,
            "timelines": urls.timelines,
            "widget": urls.widgets,
            "batchWidgets": urls.batch_widgets,
            "H5page": urls.H5page,
            "layer": urls.layer,
            "editResource": urls.edit_resource,
            "recognize": urls.recognize,
            "templateList": urls.template_list,
            "widgetTimelines": urls.widgetTimelines,
            "previewQRcode": urls.previewQRcode
        };

        RequestHelper.prototype.send = function (){
            var me = this,
                url = this.url,
                extraParams = this.extraParams;

            // admin查看AR进入不保存数据
            if (this.permission == consts.PERMISSION_READONLY && this.method != "get"){
                return false;
            }

            eventHelper.trigger(consts.EVENTS.REQUEST_CHANGED, ["add", this.uuid]);

            // 过滤额外属性
            this.params = getRemovedAttrsParams(this.params);

            // 附加url字段
            url = getUrlByExtraParams(url, extraParams);

            var xhr = $.ajax({
                url: url,
                type: "json",
                data: this.params,
                method: this.method,
                success: function (result){
                    eventHelper.trigger(consts.EVENTS.REQUEST_CHANGED, ["remove", me.uuid]);
                    me.success(result);
                },
                error: function (info){
                    eventHelper.trigger(consts.EVENTS.REQUEST_CHANGED, ["remove", me.uuid]);
                    me.error(info);
                },
                beforeSend: function(){
                    me.beforeSend();
                }
            });

            return xhr;
        };

        return RequestHelper;
  })();


  module.exports = RequestHelper;
